package com.syh.jn.scm.service.impl.operation.system;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.exception.IllegalParameterException;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.secret.PasswordUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.operation.entity.system.OperationAccount;
import com.syh.jn.scm.domain.operation.repository.system.OperationAccountRepository;
import com.syh.jn.scm.dto.operation.system.account.OperationAccountMto;
import com.syh.jn.scm.dto.operation.system.account.OperationAccountSto;
import com.syh.jn.scm.dto.operation.system.account.OperationAccountVo;
import com.syh.jn.scm.service.api.operation.system.OperationAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class OperationAccountServiceImpl extends AbstractServiceImpl<OperationAccountSto, OperationAccountMto, OperationAccountVo, OperationAccount, Long> implements OperationAccountService {

  @Autowired
  private OperationAccountRepository operationAccountRepository;


  @Override
  protected AggregateRepository<OperationAccount, Long> getAggregateRepository() {
    return this.operationAccountRepository;
  }

  @Override
  public Long save(OperationAccountSto operationAccountSto) {
    OperationAccount operationAccount = new OperationAccount(operationAccountSto);
    return operationAccountRepository.add(operationAccount);
  }

  @Override
  public void update(OperationAccountMto mto) {
    ExtAssert.notNull(mto, "对象为空");
    ExtAssert.notNull(mto.getId(), "id为空");
    OperationAccount operationAccount = operationAccountRepository.getById(mto.getId());
    if (operationAccount != null) {
      operationAccount.modify(mto);
      operationAccountRepository.update(operationAccount);
    }
  }

  @Override
  public void delete(Long id) {
    ExtAssert.notNull(id, "id为空");
    OperationAccount operationAccount = operationAccountRepository.getById(id);
    if (operationAccount != null) {
      operationAccount.destroy();
    }
    operationAccountRepository.removeById(id);
  }


  @Override
  public void changeEnable(Long accountId, Short enable) {
    ExtAssert.notNull(accountId, "id为空");
    OperationAccount operationAccount = operationAccountRepository.getById(accountId);
    if (operationAccount != null) {
      operationAccount.changeEnable(enable);
      operationAccountRepository.update(operationAccount);
    }
  }


  @Override
  public void setRole(Long accountId, Long roldId) {
    ExtAssert.notNull(roldId, "id为空");
    OperationAccount operationAccount = operationAccountRepository.getById(accountId);
    if (operationAccount != null) {
      operationAccount.changeRole(roldId);
      operationAccountRepository.update(operationAccount);
    }
  }

  @Override
  public OperationAccountVo verifyAccountPassword(String loginAccount, String password) {
    ExtAssert.notNull(loginAccount, "loginAccount为空");
    ExtAssert.notNull(password, "密码为空");
    Wrapper wrapper = Wrapper.build();
    wrapper.append(Restrictions.eq("loginAccount", loginAccount));
    OperationAccount operationAccount = operationAccountRepository.getOne(wrapper);
    if (operationAccount == null) {
      return null;
    }
    if (PasswordUtils.verify(password, operationAccount.getLoginPassword())) {
      OperationAccountVo operationAccountVo = new OperationAccountVo();
      ExtBeanUtils.copyProperties(operationAccount, operationAccountVo);
      return operationAccountVo;
    } else {
      return null;
    }
  }

  @Override
  public void updataPassword(String loginAccount, String oldPassword, String newPassword) throws IllegalParameterException {
    ExtAssert.notNull(loginAccount, "loginAccount为空");
    ExtAssert.notNull(oldPassword, "旧密码为空");
    ExtAssert.notNull(newPassword, "新密码为空");
    Wrapper wrapper = Wrapper.build();
    wrapper.append(Restrictions.eq("loginAccount", loginAccount));
    OperationAccount operationAccount = operationAccountRepository.getOne(wrapper);
    ExtAssert.notNull(operationAccount, "找不到该账户");
    if (PasswordUtils.verify(oldPassword, operationAccount.getLoginPassword())) {
      operationAccount.setLoginPassword(newPassword);
      operationAccountRepository.update(operationAccount);
    } else {
      throw new IllegalParameterException("旧密码不正确");
    }
  }

  @Override
  public void initOperationSystemAccount() {
    Wrapper wrapper = Wrapper.build();
    wrapper.append(Restrictions.eq("loginAccount", "system@syhvip"));
    OperationAccount operationAccount = operationAccountRepository.getOne(wrapper);
    if (operationAccount == null) {
      OperationAccount superAdmin = OperationAccount.createdSuperAdminOnce();
      this.operationAccountRepository.add(superAdmin);
    }
  }

  @Override
  public boolean identityAdmin(Long id) {
    Short admin = operationAccountRepository.getAdminById(id);
    if (admin != null && admin.equals(Short.valueOf("1"))) {
      return true;
    }
    return false;
  }

}
